<?php

if (!defined("BASEPATH"))
    exit("No direct script access allowed");
require_once APPPATH . "controllers/dienhoa-administrator/base_controller" . EXT;

class Dm_san_pham extends Base_controller {

    var $limit = 20;

    public function __construct() {
        parent::__construct();
        $this->check_login();
    }

    public function tat_ca_dm() {

        $this->permissions_view();
        $data = array();
        //set menu
        $data = $this->SetMenu("DanhMuc");

        $total_seg = $this->uri->total_segments();
        $data["stt"] = $this->uri->segment(4);
        $data["cateID"] = $this->uri->segment(5);
//config pagination

        if (isset($_POST["btnSend"]) || $this->input->get("pg") === "search") {
            if (isset($_POST["btnSend"])) {
                $TSP = $this->input->post("txtName", TRUE);
            }
            if ($this->input->get("pg") === "search") {
                $TSP = ($this->input->get("name", TRUE)) ? $this->input->get("name", TRUE) : NULL;
            }
            $base_url = base_url() . "dienhoa-administrator/dm_san_pham/tat_ca_dm/?pg=search&MHD=" . $TSP;
            if (isset($data["stt"]) && $data["stt"] != NULL)
                $result = $this->product_model->GetSearchCategory($data["stt"], NULL, $this->limit, $this->input->get("page"));
            else
                $result = $this->product_model->GetSearchCategory($TSP, NULL, $this->limit, $this->input->get("page"));
        } else if (!isset($_POST["btnSend"]) || $this->input->get("pg") === "hd") {
            $base_url = base_url() . "dienhoa-administrator/dm_san_pham/tat_ca_dm/?pg=hd";
            if (isset($data["stt"]) && $data["stt"] != NULL)
                $result = $this->product_model->GetSearchCategory($data["stt"], NULL, $this->limit, $this->input->get("page"));
            else
                $result = $this->product_model->GetSearchCategory(NULL, NULL, $this->limit, $this->input->get("page"));
        }

        $data["dm_sp"] = $result["rows"]->result();
        $data['num_results'] = $result['num_rows'];
        //check danh muc co san pham hay khong
        $data["checkDM"] = $this->product_model->checkDM("PID")->result();
//cofig
        $config = array();
        $config['base_url'] = $base_url;
        $config['total_rows'] = $data['num_results'];
        $config['per_page'] = $this->limit;
        $config["page_query_string"] = TRUE;
        $config["query_string_segment"] = "page";
        $this->cfPag();
        $data["m_menu"] = $this->menu_model->getMenu()->result();
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $this->render($this->load->view("admin/danh muc/dm_san_pham_view", $data, TRUE));
    }

    public function xoa() {
        $this->permission_del_edit();
        $dat = $this->input->post("item");
        for ($i = 0; $i < sizeof($dat); $i++) {
            $delete = $this->product_model->DeleteDM($dat[$i]);
            if (!$delete) {
                echo "<script>alert('Không xóa được!'); window.history.back();</script>";
            }
        }
        redirect("dienhoa-administrator/dm_san_pham/tat_ca_dm", "refresh");
    }

    //view add danh muc
    public function formThemDm() {
        $this->permissions_view();

        $data = array();
        $data = $this->SetMenu("DanhMuc");
        $data["m"] = $this->menu_model->getMenu()->result();
        $this->render($this->load->view("admin/danh muc/them_danh_muc_view", $data, TRUE));
    }

    //them danh muc
    public function ThemDm() {

        $this->permission_del_edit();
        $data = array();
        //kiem tra truong nhap lieu
        $config = array(
            array(
                'field' => 'txtName',
                'label' => 'Tên danh mục',
                'rules' => 'trim|required|xss_clean'
            ),
            array(
                'field' => 'CatStatus',
                'label' => 'menu',
                'rules' => 'required'
            )
        );

        $this->form_validation->set_rules($config);
        if ($this->form_validation->run() === false) {
            $this->formThemDm();
        } else {
            $query = array(
                "txtName" => $this->input->post('txtName'),
                "stt" => $this->input->post("CatStatus")
            );
            $query["slug"] = mb_strtolower(url_title(removesign($query["txtName"])));
            $data["addDM"] = $this->product_model->addDM($query);
            if ($data["addDM"]) {
                redirect("dienhoa-administrator/dm_san_pham/tat_ca_dm", "refresh");
            } else {
                echo "<script>alert('Thêm thất bại, xin hãy thử lại!');window.history.back();</script>";
            }
        }
    }

    //chi tiet danh muc
    public function chi_tiet_danh_muc() {

        $this->permissions_view();
        $data = array();
        $data = $this->SetMenu("DanhMuc");
        $pid = $this->uri->segment(4);
        $data["dm_view"] = $this->product_model->GetCategory($pid)->row();
        $data["m_menu"] = $this->menu_model->getMenu()->result();
        $this->render($this->load->view("admin/danh muc/chi_tiet_dm_view", $data, TRUE));
    }

    public function sua_danh_muc() {

        $this->permission_del_edit();
        $pid = $this->uri->segment(4);
        $data = array();
        //kiem tra truong nhap lieu
        $config = array(
            array(
                'field' => 'txtName',
                'label' => 'Tên danh mục',
                'rules' => 'trim|required|xss_clean'
            ),
            array(
                'field' => 'MenuStatus',
                'label' => 'menu',
                'rules' => 'required|integer'
            ),
            array(
                'field' => 'CatStatus',
                'label' => 'Trạng thái danh mục',
                'rules' => 'required|integer'
            )
        );

        $this->form_validation->set_rules($config);
        if ($this->form_validation->run() === false) {
            $this->chi_tiet_danh_muc();
        } else {
            $query = array(
                "txtName" => $this->input->post('txtName'),
                "MStatus" => $this->input->post("MenuStatus"),
                "CatStatus" => $this->input->post('CatStatus')
            );
            $query["slug"] = mb_strtolower(url_title(removesign($query["txtName"])));
            $data["upDM"] = $this->product_model->UpdateCat($query, $pid);
            if ($data["upDM"]) {
                redirect("dienhoa-administrator/dm_san_pham/tat_ca_dm", "refresh");
            } else {
                echo "<script>alert('Sửa thất bại, xin hãy thử lại!');window.history.back();</script>";
            }
        }
    }

}

?>
